/************************* 类描述 *********************************
 * 礼物记录服务层
 *****************************************************************
 */
package com.realgodo.site.service.application;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.application.present.PresentRecord;
import com.realgodo.site.utils.form.PresentRecordForm;

@Service
@Transactional
public class PresentRecordManager extends DefaultEntityManager<PresentRecord, Long> {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * 礼物通用查询
	 * 
	 * @param aPresentRecordPages
	 * @param presentForm
	 * @return
	 */
	public Page<PresentRecord> findShows(
			Page<PresentRecord> aPresentRecordPages,
			PresentRecordForm aPresentRecordForm) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				PresentRecord.class);
		if (aPresentRecordForm != null
				&& aPresentRecordForm.getReceiverId() != null) {
			criteria.add(Restrictions.eq("space.id", aPresentRecordForm
					.getReceiverId()));
		}
		if (aPresentRecordForm != null
				&& aPresentRecordForm.getSenderId() != null) {
			criteria.add(Restrictions.eq("sender.id", aPresentRecordForm
					.getReceiverId()));
		}
		criteria.addOrder(Order.desc("id"));
		return this.entityDao.findByCriteria(aPresentRecordPages, criteria);
	}
}
